package com.walixiwa.player;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.dueeeke.videocontroller.component.CompleteView;
import com.dueeeke.videocontroller.component.ErrorView;
import com.dueeeke.videoplayer.player.VideoView;
import com.walixiwa.player.utils.VodPositionManager;
import com.walixiwa.player.widget.DetailGestureView;
import com.walixiwa.player.widget.DetailLoadingView;
import com.walixiwa.player.widget.DetailVodControlView;
import com.walixiwa.player.widget.DetailVodController;
import com.walixiwa.player.widget.DetailVodEpisodeView;
import com.walixiwa.player.widget.DetailVodScaleView;
import com.walixiwa.player.widget.DetailVodSpeedView;
import com.walixiwa.player.widget.DetailVodTitleView;
import com.walixiwa.videoviewcontral.R;

import java.util.ArrayList;
import java.util.List;

public class VideoViewControl {
    private Context context;
    private boolean enableToast = true;
    private List<String> titles = new ArrayList<>();
    private List<String> urls = new ArrayList<>();
    private int mCurrentVideoPosition = 0;

    private VideoView videoView;
    private DetailVodController controller;
    private DetailVodSpeedView speedView;
    private DetailVodEpisodeView episodeView;
    private DetailVodTitleView titleView;
    private DetailVodScaleView scaleView;
    private DetailVodControlView vodControlView;
    private DetailLoadingView loadingView;


    public VideoViewControl with(VideoView videoView) {
        VodPositionManager.init(videoView.getContext());
        this.urls.clear();
        this.titles.clear();
        this.videoView = videoView;
        this.context = videoView.getContext();
        this.controller = new DetailVodController(videoView.getContext());

        this.controller.setEnableOrientation(false);
        //竖屏也开启手势操作，默认关闭
        this.controller.setEnableInNormal(false);
        //滑动调节亮度，音量，进度，默认开启
        this.controller.setGestureEnabled(true);
        //适配刘海屏，默认开启
        //controller.setAdaptCutout(false);

        //准备播放界面
        this.loadingView = new DetailLoadingView(context);
       /* ImageView thumb = prepareView.findViewById(R.id.thumb);//封面图
        Glide.with(this).load(THUMB).into(thumb);*/
        controller.addControlComponent(loadingView);

        this.speedView = new DetailVodSpeedView(context);

        this.titleView = new DetailVodTitleView(context);
        this.controller.addControlComponent(titleView);

        this.titleView.getSizeButton().setVisibility(View.GONE);
        this.titleView.getSpeedButton().setVisibility(View.GONE);
        this.titleView.getEpisodeButton().setVisibility(View.GONE);

        this.episodeView = new DetailVodEpisodeView(context);
        this.scaleView = new DetailVodScaleView(context);

        this.vodControlView = new DetailVodControlView(context);

        DetailGestureView gestureControlView = new DetailGestureView(context);//滑动控制视图
        this.controller.addControlComponent(gestureControlView);

        this.videoView.setVideoController(controller);

        this.videoView.setOnStateChangeListener(new VideoView.SimpleOnStateChangeListener() {
            @Override
            public void onPlayStateChanged(int playState) {
                if (playState == VideoView.STATE_PLAYBACK_COMPLETED) {
                    mCurrentVideoPosition++;
                    if (mCurrentVideoPosition >= urls.size()) {
                        showToast("已是最后一集");
                        return;
                    }
                    if (onEpisodeChangedListener != null) {
                        onEpisodeChangedListener.onEpisodeChanged(mCurrentVideoPosition);
                    }

                    showToast("开始播放：" + VideoViewControl.this.titles.get(mCurrentVideoPosition));
                    VideoViewControl.this.videoView.release();
                    VideoViewControl.this.videoView.setUrl(urls.get(mCurrentVideoPosition));
                    VideoViewControl.this.titleView.setTitle(titles.get(mCurrentVideoPosition));
                    VideoViewControl.this.videoView.setVideoController(controller);
                    //开始播放
                    VideoViewControl.this.videoView.start();
                }
                if (playState == VideoView.STATE_PREPARING) {
                    VideoViewControl.this.titleView.getSpeedButton().setText(String.format("%s倍速", "x1.00"));
                    VideoViewControl.this.speedView.reset();
                }
                if (playState == VideoView.STATE_PLAYING) {
                    if (onStartPlayListener != null) {
                        onStartPlayListener.OnStartPlay();
                    }
                }
                Log.e("Video", "mCurrentVideoPosition: " + mCurrentVideoPosition);
                VideoViewControl.this.episodeView.setChecked(mCurrentVideoPosition);
            }

        });

        return this;
    }

    private void showToast(String msg) {
        if (enableToast) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 根据屏幕方向自动进入/退出全屏
     */
    public VideoViewControl enableOrientation() {
        controller.setEnableOrientation(true);
        return this;
    }


    /**
     * 添加complete布局
     */
    public VideoViewControl enableCompleteView() {
        controller.addControlComponent(new CompleteView(context));//自动完成播放界面
        return this;
    }

    /**
     * 添加Error布局
     */
    public VideoViewControl enableErrorView() {
        controller.addControlComponent(new ErrorView(context));//错误界面
        return this;
    }

    /**
     * 启用屏幕缩放功能
     *
     * @return
     */
    public VideoViewControl enableSpeedView() {
        controller.addControlComponent(speedView);//倍速界面
        titleView.getSpeedButton().setVisibility(View.VISIBLE);
        titleView.getSpeedButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speedView.show();
            }
        });
        speedView.setOnSpeedClickListener(new DetailVodSpeedView.OnSpeedClickListener() {
            @Override
            public void onSpeedClick(String name, float speed) {
                titleView.getSpeedButton().setText(String.format("%s倍速", name));
            }
        });
        return this;
    }

    /**
     * 启用选集功能
     *
     * @return
     */
    public VideoViewControl enableEpisodeView() {
        controller.addControlComponent(episodeView);//剧集界面
        titleView.getEpisodeButton().setVisibility(View.VISIBLE);
        titleView.getEpisodeButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                episodeView.show();
            }
        });
        episodeView.setOnItemClickListener(new DetailVodEpisodeView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mCurrentVideoPosition = position;
                if (mCurrentVideoPosition >= urls.size()) {
                    showToast("已是最后一集");
                    return;
                }
                if (onEpisodeChangedListener != null) {
                    onEpisodeChangedListener.onEpisodeChanged(mCurrentVideoPosition);
                }
                //VideoViewControl.this.episodeView.setChecked(mCurrentVideoPosition);
                showToast("开始播放：" + VideoViewControl.this.titles.get(mCurrentVideoPosition));
                videoView.release();
                videoView.setUrl(urls.get(mCurrentVideoPosition));
                titleView.setTitle(titles.get(mCurrentVideoPosition));
                videoView.setVideoController(controller);
                //开始播放
                videoView.start();
            }
        });

        return this;
    }


    public VideoViewControl enableScaleView() {
        //画面缩放
        controller.addControlComponent(scaleView);
        titleView.getSizeButton().setVisibility(View.VISIBLE);
        titleView.getSizeButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scaleView.show();
            }
        });
        scaleView.setOnScaleClickListener(new DetailVodScaleView.OnScaleClickListener() {
            @Override
            public void onScaleClick(String name) {
                titleView.getSizeButton().setText(name);
            }
        });
        return this;
    }

    public VideoViewControl enableVodControlView() {
        //点播控制条
        controller.addControlComponent(vodControlView);
        return this;
    }

    public void setTitles(List<String> titles) {
        VideoViewControl.this.titles.clear();
        VideoViewControl.this.titles.addAll(titles);
        if (episodeView != null) {
            episodeView.setVodNameList(titles);
        }
    }

    public void setUrls(List<String> urls) {
        VideoViewControl.this.urls.clear();
        VideoViewControl.this.urls.addAll(urls);
        if (vodControlView != null && urls.size() > 1) {
            vodControlView.enableNextButton(true, new DetailVodControlView.OnNextClickListener() {
                @Override
                public void onNextClick() {
                    mCurrentVideoPosition++;
                    if (mCurrentVideoPosition >= VideoViewControl.this.urls.size()) {
                        showToast("已是最后一集");
                        return;
                    }
                    if (onEpisodeChangedListener != null) {
                        onEpisodeChangedListener.onEpisodeChanged(mCurrentVideoPosition);
                    }
                    //VideoViewControl.this.episodeView.setChecked(mCurrentVideoPosition);
                    showToast("开始播放：" + VideoViewControl.this.titles.get(mCurrentVideoPosition));
                    VideoViewControl.this.videoView.release();
                    VideoViewControl.this.videoView.setUrl(VideoViewControl.this.urls.get(mCurrentVideoPosition));
                    VideoViewControl.this.titleView.setTitle(VideoViewControl.this.titles.get(mCurrentVideoPosition));
                    VideoViewControl.this.videoView.setVideoController(controller);
                    //开始播放
                    VideoViewControl.this.videoView.start();
                }
            });
        }
    }

    public ImageView getThumbView() {
        return loadingView.findViewById(R.id.thumb);
    }

    public VideoViewControl enableProgressManager() {
        this.videoView.setProgressManager(new DetailProgressManager());
        return this;
    }

    public void autoPlay(int position) {
        mCurrentVideoPosition = position;
        if (mCurrentVideoPosition >= VideoViewControl.this.urls.size()) {
            showToast("要播放的剧集位置超出可播放剧集数");
            return;
        }
        if (onEpisodeChangedListener != null) {
            onEpisodeChangedListener.onEpisodeChanged(mCurrentVideoPosition);
        }
        this.episodeView.setChecked(mCurrentVideoPosition);
        showToast("开始播放：" + VideoViewControl.this.titles.get(mCurrentVideoPosition));
        VideoViewControl.this.videoView.release();
        VideoViewControl.this.videoView.setUrl(VideoViewControl.this.urls.get(mCurrentVideoPosition));
        VideoViewControl.this.titleView.setTitle(VideoViewControl.this.titles.get(mCurrentVideoPosition));
        VideoViewControl.this.videoView.setVideoController(controller);
        //开始播放
        VideoViewControl.this.videoView.start();
    }


    public void setEnableToast(boolean enableToast) {
        this.enableToast = enableToast;
    }

    public List<String> getTitles() {
        return titles;
    }

    public List<String> getUrls() {
        return urls;
    }

    public int getmCurrentVideoPosition() {
        return mCurrentVideoPosition;
    }

    public VideoView getVideoView() {
        return videoView;
    }

    public DetailVodController getController() {
        return controller;
    }

    public DetailVodSpeedView getSpeedView() {
        return speedView;
    }

    public DetailVodEpisodeView getEpisodeView() {
        return episodeView;
    }

    public DetailVodTitleView getTitleView() {
        return titleView;
    }

    public DetailVodScaleView getScaleView() {
        return scaleView;
    }

    public DetailVodControlView getVodControlView() {
        return vodControlView;
    }

    public DetailLoadingView getLoadingView() {
        return loadingView;
    }

    private OnEpisodeChangedListener onEpisodeChangedListener;

    public void setOnEpisodeChangedListener(OnEpisodeChangedListener onEpisodeChangedListener) {
        this.onEpisodeChangedListener = onEpisodeChangedListener;
    }

    public interface OnEpisodeChangedListener {
        void onEpisodeChanged(int position);
    }

    private OnStartPlayListener onStartPlayListener;

    public void setOnStartPlayListener(OnStartPlayListener onStartPlayListener) {
        this.onStartPlayListener = onStartPlayListener;
    }

    public interface OnStartPlayListener {
        void OnStartPlay();
    }
}
